<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Card extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getCardsPage()
    {
        $user = $this->auth->getUser();

        $storeAndRoomInfo = model('Store')->with('rooms')->select();
        // 过滤掉房间状态为下线的
        $newStoreAndRoomInfo = [];
        foreach ($storeAndRoomInfo as $sr) {
            $haveOne = false;
            if ($sr->rooms && count($sr->rooms)) {
                foreach ($sr->rooms as $r) {
                    if ($r->status == 'normal') {
                        $haveOne = true;
                    }
                }
            }
            if ($haveOne) {
                $newStoreAndRoomInfo[] = $sr;
            }
        }

//        var_dump($storeAndRoomInfo);

        $params = $this->request->get();

        if (in_array($user->group_id, [2, 3])) {
            $cards = [];
        } else {
            $cards = model('Card')->where('card_display', 1)->order('weigh', 'desc')->select();
            $cards = array_map(function ($e) {
                // $rooms = \app\admin\model\Room::all();
                // 增加过滤状态
                $rooms = \app\admin\model\Room::where('status', 'normal')->select();
                $roomIds = array_map(function ($e) {
                    return $e['id'];
                }, $rooms);

                $setRoomIds = explode(',', $e->store_ids);
                // temp 还原store_ids存储为门店id,便于前端筛选
                // 增加过滤状态
                $rooms = \app\admin\model\Room::where('id', 'in', $setRoomIds)->where('status', 'normal')->select();
                $storeIds = array_map(function ($e) {
                    return $e['store_id'];
                }, $rooms);
                // 去重下
                $storeIds = array_flip($storeIds);
                $storeIds = array_flip($storeIds);

                // 过滤状态的教室id
                $filterRoomIds = array_map(function ($e) {
                    return $e['id'];
                }, $rooms);

                $count = count($filterRoomIds);
                if ($count == count($roomIds)) {
                    $e->display_store_string = '适用全部教室';
                } else {
                    $e->display_store_string = "适用${count}个门店";
                }
                $e->store_ids = "'" . implode("','", $storeIds) . "'";
                $e->roomsCount = 1;

                return $e;
            }, $cards);
        }

        $this->success('ok', [
            'store_and_room_info' => $newStoreAndRoomInfo,
            'cards' => $cards,
        ]);
    }

    public function list()
    {
        $params = $this->request->get();
        $data = model('Card')->with(['store', 'room'])->select();
//        var_dump($data);

//        $cardInfo = array_map(function ($e) {
//            return [
//                'id' => $e->id,
//                'name' => $e->card_name,
//                'price' => $e->card_price,
//                'store' => '适用门店:' . is_null($e->store) ? '通用' : $e->store->store_name,
//                'room' => '适用教室：通用',
//                'card_checknum' => '卡：12次',
//                'card_max_hour' => '每次时长：2小时',
//            ];
//        }, $data);
        $this->success('ok', $data);

    }

    public function getCardInfo()
    {
        $id = $this->request->get('id');
        $data = model('Card')->where('id', $id)->find();



        $rooms = \app\admin\model\Room::where('id', 'in', $data->store_ids)->select();
        $storeNames =  [];
        foreach ($rooms as $room) {
            $store = \app\admin\model\Store::get($room->store_id);
            $storeNames[] = $store->store_name;
        }

        $data->roomsCount = count(explode(',', $data->store_ids));
        $data->storesString = implode(',', $storeNames);

        $this->success('ok', $data);

    }
}
